package com.mgq.java8.stream;

import org.springframework.boot.autoconfigure.amqp.RabbitProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>功能：</b>测试flatMap<br>
 * <b>Copyright TCSL</b>
 * <li>版本&nbsp;&nbsp;&nbsp;&nbsp;修改日期&nbsp;&nbsp;&nbsp;&nbsp;部　　门&nbsp;&nbsp;&nbsp;&nbsp;作　者&nbsp;&nbsp;&nbsp;&nbsp;变更内容</li><hr>
 * <li>v1.0&nbsp;&nbsp;&nbsp;&nbsp;20220414&nbsp;&nbsp;营业后台&nbsp;&nbsp;&nbsp;&nbsp;马广奇&nbsp;&nbsp;&nbsp;&nbsp;创建类</li>
 * <br><br>
 *
 * @author 马广奇
 * @version 2022-04-14 11:09
 */
public class FlatMap {
    public static void main(String[] args) throws IOException {
        testFlatMap();
    }

    public static void testFlatMap() throws IOException {
        String[] hello = {"hello", "world"};
        Stream<String> stream = Arrays.stream(hello);

        List<String> list = Arrays.asList("HELLO", "WORLD");
        List<String> collect = list.stream().map(word -> word.split(""))
                //.flatMap(Stream::of) 2个相同,都是产生流
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        collect.forEach(System.out::println);
        System.out.println("-------------");
        Stream<String> lines = Files.lines(Path.of("1.txt",""), StandardCharsets.UTF_8);
        //传递给flatMap的mapper函数使用简单的正则表达式将一行拆分为一个单词数组，然后从该数组创建一个单词流。
        lines.flatMap(i-> Stream.of(i.split(" +"))).forEach(System.out::println);
    }
}
